package org.puppet.game.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.puppet.game.Controlable;
import org.puppet.game.EventCode;
import org.puppet.game.World;
import org.puppet.game.control.Director;
import org.puppet.game.control.TouchController;
import org.puppet.game.element.Puppet;
import org.puppet.game.map.SceneMap;

import android.content.Context;
import android.content.Resources;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SceneView extends View implements Controlable {

	public final static int EVENT_DRAW = EventCode.code++;
	
    protected Resources resources;
    protected Director director;
    protected List<Puppet> puppets = new ArrayList<Puppet>();
    protected SceneMap map;
    
	public SceneView(Context context, AttributeSet attrs, Map inflateParams, int defStyle) {
		super(context, attrs, inflateParams, defStyle);
		init();
	}
	
	public SceneView(Context context, AttributeSet attrs, Map inflateParams) {
		super(context, attrs, inflateParams);
		init();
	}
	
	public SceneView() {
		this(World.current);
	}
	
	public SceneView(Context context) {
		super(context);
		init();
	}
	
	private void init(){
        resources = getResources();
        
        director = new Director();
        director.reg(new TouchController(this));
	}
	
	public void addPuppet(Puppet puppet){
		puppets.add(puppet);
	}
	
	public void deletePuppet(Puppet puppet){
		puppets.remove(puppet);
	}

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	return director.broadcast(EventCode.MOTION, event);
    }

	@Override
    protected void onDraw(Canvas canvas) {
    	 super.onDraw(canvas);
    	 map.draw(canvas);
    	 director.broadcast(EVENT_DRAW, canvas);
    }

	public Director getDirector() {
		return director;
	}

	public List<Puppet> getPuppets() {
		return puppets;
	}
	
	public void setSceneMap(SceneMap map){
		this.map=map;
		setBackground(map.getBackground());
	}
	public SceneMap getSceneMap() {
		return map;
	}
}
